import 'dart:convert';
import 'dart:io';
import 'package:crypto/crypto.dart';
import 'package:dio/dio.dart';

String byteArrayToHexString(List<int> byteArray) {
  StringBuffer stringBuffer = StringBuffer();
  for (int i = 0; i < byteArray.length; i++) {
    stringBuffer.write(byteArray[i].toRadixString(16).padLeft(2, '0'));
  }
  return stringBuffer.toString();
}

Future<void> main() async {
  String paramString1 = "qq";
  String type = "mp3";
  String paramString2 = "0022IrcT3UFwmR";
  String str2 = "Redmi";
  String str3 = "11";
  int l = DateTime.now().millisecondsSinceEpoch ~/ 1000;
  // l = 1701746442;
  String stringBuilder1 = "d86b856be4a7ea7a5bc9b6c4eed46f4e$l"
      "6562653262383463363633646364306534333668";
  String str1 =
      md5.convert(utf8.encode(stringBuilder1)).toString().toLowerCase();
  StringBuffer stringBuilder2 = StringBuffer();
  stringBuilder2
      .write("{\\\"method\\\":\\\"GetMusicUrl\\\",\\\"platform\\\":\\\"");
  stringBuilder2.write(paramString1);
  stringBuilder2.write("\\\",\\\"t1\\\":\\\"");
  stringBuilder2.write(paramString2);
  stringBuilder2.write("\\\",\\\"t2\\\":\\\"");
  stringBuilder2.write(type);
  stringBuilder2.write("\\\"}");
  paramString2 = stringBuilder2.toString();
  StringBuffer stringBuilder4 = StringBuffer();
  stringBuilder4.write(
      "{\\\"uid\\\":\\\"\\\",\\\"token\\\":\\\"\\\",\\\"deviceid\\\":\\\"84ac82836212e869dbeea73f09ebe52b\\\",\\\"appVersion\\\":\\\"");
  stringBuilder4.write("4.1.4");
  stringBuilder4.write("\\\",\\\"vercode\\\":\\\"");
  stringBuilder4.write("4140");
  stringBuilder4.write("\\\",\\\"device\\\":\\\"");
  stringBuilder4.write(str2);
  stringBuilder4.write("\\\",\\\"osVersion\\\":\\\"");
  stringBuilder4.write(str3);
  stringBuilder4.write("\\\"}");
  String str5 = stringBuilder4.toString();
  StringBuffer stringBuilder5 = StringBuffer();
  stringBuilder5.write("{\n\t\"text_1\":\t\"");
  stringBuilder5.write(paramString2);
  stringBuilder5.write("\",\n\t\"text_2\":\t\"");
  stringBuilder5.write(str5);
  stringBuilder5.write("\",\n\t\"sign_1\":\t\"");
  stringBuilder5.write(str1);
  stringBuilder5.write("\",\n\t\"time\":\t\"");
  stringBuilder5.write(l);
  stringBuilder5.write("\",\n\t\"sign_2\":\t\"");
  StringBuffer stringBuilder6 = StringBuffer();
  stringBuilder6.write(paramString2.replaceAll("\\", ""));
  stringBuilder6.write(str5.replaceAll("\\", ""));
  stringBuilder6.write(str1);
  stringBuilder6.write(l);
  stringBuilder6.write("NDRjZGIzNzliNzEe");
  stringBuilder5.write(md5
      .convert(utf8.encode(stringBuilder6.toString()))
      .toString()
      .toLowerCase());
  stringBuilder5.write("\"\n}");
  ZLibCodec codec = ZLibCodec();
  List<int> abc = codec.encode(utf8.encode(
      byteArrayToHexString(utf8.encode(stringBuilder5.toString()))
          .toUpperCase()));
  final dio = Dio();
  final rs = await dio.post(
    'http://app.kzti.top/client/cgi-bin/api.fcg',
    data: abc,
    options: Options(responseType: ResponseType.bytes),
  );
  List<int> decompressedData = codec.decode(rs.data);
  String ru = utf8.decode(decompressedData);
  print(ru);
}
